package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.Office;
import com.wgchao.identityrightsmanagement.domain.organization.repository.OfficeRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.OfficeConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseOfficeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: OfficeDao
 * @author: wgchao
 * @createTime: 2020/6/16 4:45 PM
 * @Description: TODO
 */
@Component
public class OfficeDao implements OfficeRepository {
    @Autowired
    private SysBaseOfficeRepository sysBaseOfficeRepository;
    @Autowired
    private OfficeConvert officeConvert;


    @Override
    public String generateCode() {
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        return sdf.format(new Date())+UUID.randomUUID().toString().split("-")[2].toUpperCase();
    }

    @Override
    public Office findById(String s) {
        SysBaseOffice sysBaseOffice = sysBaseOfficeRepository.findFirstByCodeAndDelFalse(s);
        if(sysBaseOffice == null){
            return null;
        }
        return officeConvert.convert(sysBaseOffice);
    }

    @Override
    public void remove(Office office, String operator) {
        SysBaseOffice sysBaseOffice = sysBaseOfficeRepository.findFirstByCodeAndDelFalse(office.getCode());
        if(sysBaseOffice == null){
            throw new ObjectNotExistException("职位不存在");
        }
        sysBaseOffice.setDel(true);
        sysBaseOffice.setLastUpdateTime(new Date());
        sysBaseOffice.setLastUpdator(operator);
        sysBaseOffice = sysBaseOfficeRepository.save(sysBaseOffice);
    }

    @Override
    public void add(Office office, String operator) {
        if(office.getId().compareTo(0L) < 0){//新增
            SysBaseOffice sysBaseOffice = sysBaseOfficeRepository.findFirstByCodeAndDelFalse(office.getCode());
            if(sysBaseOffice != null){
                throw new ObjectExistException("职位标识已被占用");
            }
            sysBaseOffice = officeConvert.convert(office);
            sysBaseOffice.setDel(false);
            sysBaseOffice.setCreator(operator);
            sysBaseOffice.setCreateTime(new Date());
            sysBaseOffice.setId(null);
            sysBaseOffice = sysBaseOfficeRepository.save(sysBaseOffice);
        }else{//修改
            SysBaseOffice sysBaseOffice_old = sysBaseOfficeRepository.findFirstByCodeAndDelFalse(office.getCode());
            if(sysBaseOffice_old == null){
                throw new ObjectNotExistException("职位不存在");
            }
            SysBaseOffice sysBaseOffice = officeConvert.convert(office);
            sysBaseOffice.setCreateTime(sysBaseOffice_old.getCreateTime());
            sysBaseOffice.setCreator(sysBaseOffice_old.getCreator());
            sysBaseOffice.setLastUpdator(operator);
            sysBaseOffice.setLastUpdateTime(new Date());
            sysBaseOffice.setDel(sysBaseOffice_old.getDel());
            sysBaseOffice = sysBaseOfficeRepository.save(sysBaseOffice);
        }
    }

    @Override
    public void addAll(List<Office> offices, String operator) {
        for(Office office : offices){
            add(office, operator);
        }
    }

    @Override
    public void removeAll(List<Office> offices, String operator) {
        for(Office office : offices){
            remove(office, operator);
        }
    }
}
